<template>
  <div class="dashboard-page">
    <!-- 智能报表平台头部 -->
    <div class="header-section">
      <div class="header-gradient">
        <h1 class="header-title">智能报表平台</h1>
      </div>
    </div>
    
    <div class="page-container">
      <!-- 今日数据卡片 -->
      <div class="data-cards">
        <van-grid :column-num="2" :gutter="10" :border="false">
          <van-grid-item class="data-card">
            <div class="card-content">
              <div class="card-icon sales">
                <van-icon name="gold-coin-o" size="20" />
              </div>
              <div class="card-info">
                <div class="card-value">¥{{ salesAmount }}</div>
                <div class="card-label">今日销售额</div>
              </div>
            </div>
          </van-grid-item>
          <van-grid-item class="data-card">
            <div class="card-content">
              <div class="card-icon inventory">
                <van-icon name="records" size="20" />
              </div>
              <div class="card-info">
                <div class="card-value">{{ inventoryCount }}</div>
                <div class="card-label">库存数量</div>
              </div>
            </div>
          </van-grid-item>
        </van-grid>
      </div>

      <!-- 主要功能模块 -->
      <div class="function-modules">
        <van-grid :column-num="4" :gutter="16" :border="false">
          <van-grid-item
            v-for="module in functionModules"
            :key="module.name"
            class="module-item"
            @click="handleModuleClick(module)"
          >
            <div class="module-content">
              <div class="module-icon" :style="{ color: module.color }">
                <van-icon :name="module.icon" size="22" />
              </div>
              <div class="module-text">{{ module.label }}</div>
            </div>
          </van-grid-item>
        </van-grid>
      </div>
    </div>

    <TabBar />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { showToast } from 'vant'
import TabBar from '@/components/TabBar.vue'
import { useAppStore } from '@/stores'
import { mockApi } from '@/api'

const router = useRouter()
const appStore = useAppStore()

// 今日数据
const salesAmount = ref('156,789')
const inventoryCount = ref('2,348')

// 主要功能模块
const functionModules = ref([
  {
    name: 'purchase',
    label: '采购报表',
    icon: 'orders-o',
    color: '#FF9500'
  },
  {
    name: 'sales',
    label: '销售报表',
    icon: 'chart-trending-o',
    color: '#3B82F6'
  },
  {
    name: 'customer',
    label: '客户余额',
    icon: 'user-circle-o',
    color: '#06B6D4'
  },
  {
    name: 'inventory',
    label: '库存',
    icon: 'records',
    color: '#8B5CF6'
  },
  {
    name: 'sales-rank',
    label: '销售排行',
    icon: 'bar-chart-o',
    color: '#06B6D4'
  },
  {
    name: 'region',
    label: '区域分析',
    icon: 'location-o',
    color: '#10B981'
  },
  {
    name: 'chart-analysis',
    label: '图表分析',
    icon: 'graphic',
    color: '#F59E0B'
  },
  {
    name: 'recharge',
    label: '充值',
    icon: 'gold-coin-o',
    color: '#3B82F6'
  },
  {
    name: 'recharge-record',
    label: '充值记录',
    icon: 'balance-list-o',
    color: '#06B6D4'
  }
])

// 处理模块点击
const handleModuleClick = (module: any) => {
  switch (module.name) {
    case 'purchase':
      showToast('采购报表功能开发中...')
      break
    case 'sales':
      router.push('/charts')
      break
    case 'customer':
      showToast('客户余额功能开发中...')
      break
    case 'inventory':
      showToast('库存管理功能开发中...')
      break
    case 'sales-rank':
      router.push('/analytics')
      break
    case 'region':
      showToast('区域分析功能开发中...')
      break
    case 'chart-analysis':
      router.push('/charts')
      break
    case 'recharge':
      showToast('充值功能开发中...')
      break
    case 'recharge-record':
      router.push('/data-table')
      break
    default:
      showToast(module.label)
  }
}

// 加载数据
const loadData = async () => {
  try {
    appStore.setLoading(true)
    const data = await mockApi.getDashboardData()
    appStore.updateDashboardData(data)
    
    // 更新今日数据
    salesAmount.value = (data.totalSales / 1000).toFixed(0) + ',000'
    inventoryCount.value = '2,348'
    
  } catch (error) {
    console.error('加载仪表板数据失败:', error)
    showToast('加载数据失败')
  } finally {
    appStore.setLoading(false)
  }
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.dashboard-page {
  height: 100vh;
  height: 100dvh; /* 动态视口高度 */
  background: linear-gradient(135deg, #fafbfc 0%, #f6f8fa 100%);
  padding-bottom: max(60px, env(safe-area-inset-bottom));
  padding-top: env(safe-area-inset-top);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  position: relative;
  max-height: 100vh;
  max-height: 100dvh;
}

.dashboard-page::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: radial-gradient(circle at 20% 20%, rgba(99, 102, 241, 0.03) 0%, transparent 50%),
              radial-gradient(circle at 80% 80%, rgba(59, 130, 246, 0.03) 0%, transparent 50%);
  pointer-events: none;
}

.page-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
  z-index: 1;
}

/* 头部区域 */
.header-section {
  margin-bottom: 20px;
  flex-shrink: 0;
}

.header-gradient {
  background: linear-gradient(135deg, #1E90FF 0%, #87CEEB 100%);
  padding: 20px 16px;
  text-align: center;
  position: relative;
  overflow: hidden;
}

.header-gradient::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(45deg, transparent, rgba(255, 255, 255, 0.08), transparent);
  transform: translateX(-100%);
  animation: headerShine 4s ease-in-out infinite;
}

@keyframes headerShine {
  0% { transform: translateX(-100%); }
  50% { transform: translateX(100%); }
  100% { transform: translateX(100%); }
}

.header-title {
  color: white;
  font-size: 18px;
  font-weight: 600;
  margin: 0;
  letter-spacing: 1px;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
  position: relative;
  z-index: 1;
}

/* 数据卡片区域 */
.data-cards {
  margin: 0 16px 20px;
  flex-shrink: 0;
}

.data-card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
  border: 1px solid rgba(226, 232, 240, 0.8);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  overflow: hidden;
  position: relative;
}

.data-card:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.06);
}

.card-content {
  display: flex;
  align-items: center;
  padding: 18px;
  gap: 16px;
}

.card-icon {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  position: relative;
  overflow: hidden;
}

.card-icon::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: linear-gradient(45deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transform: rotate(45deg);
  transition: all 0.6s;
}

.card-icon:hover::before {
  animation: shimmer 1.5s ease-in-out;
}

@keyframes shimmer {
  0% { transform: translateX(-100%) translateY(-100%) rotate(45deg); }
  100% { transform: translateX(100%) translateY(100%) rotate(45deg); }
}

.card-icon.sales {
  background: linear-gradient(135deg, #10b981, #059669);
  box-shadow: 0 2px 8px rgba(16, 185, 129, 0.25);
}

.card-icon.inventory {
  background: linear-gradient(135deg, #3b82f6, #2563eb);
  box-shadow: 0 2px 8px rgba(59, 130, 246, 0.25);
}

.card-info {
  flex: 1;
}

.card-value {
  font-size: 18px;
  font-weight: 600;
  color: #1f2937;
  margin-bottom: 6px;
}

.card-label {
  font-size: 12px;
  color: #6b7280;
  font-weight: 500;
}

/* 功能模块区域 */
.function-modules {
  padding: 0 16px;
  flex: 1;
  overflow: hidden;
}

.module-item {
  background: white;
  border-radius: 12px;
  margin-bottom: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
  border: 1px solid rgba(226, 232, 240, 0.6);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
}

.module-item::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, rgba(248, 250, 252, 0.8), rgba(241, 245, 249, 0.4));
  opacity: 0;
  transition: opacity 0.3s ease;
}

.module-item:hover::before {
  opacity: 1;
}

.module-item:active {
  transform: scale(0.97) translateY(1px);
  box-shadow: 0 1px 6px rgba(0, 0, 0, 0.08);
}

.module-content {
  padding: 18px 10px;
  text-align: center;
  position: relative;
  z-index: 1;
}

.module-icon {
  margin-bottom: 10px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.06));
}

.module-item:hover .module-icon {
  transform: translateY(-1px) scale(1.05);
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.1));
}

.module-text {
  font-size: 11px;
  color: #4b5563;
  font-weight: 500;
  line-height: 1.3;
}

/* 网格样式覆盖 */
:deep(.van-grid-item__content) {
  padding: 0;
  background: transparent;
  border-radius: 12px;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

:deep(.van-grid-item__content--center) {
  justify-content: center;
  text-align: center;
}

/* 确保 van-grid 正常显示 */
:deep(.van-grid) {
  background: transparent;
}

:deep(.van-grid-item) {
  background: transparent;
}

/* 响应式适配 */
@media screen and (max-width: 350px) {
  .header-gradient {
    padding: 16px 12px;
  }
  
  .header-title {
    font-size: 16px;
  }
  
  .module-content {
    padding: 12px 6px;
  }
  
  .module-text {
    font-size: 10px;
  }
  
  .card-content {
    padding: 14px;
  }
  
  .card-value {
    font-size: 16px;
  }
  
  .function-modules {
    padding: 0 8px;
  }
}

/* 高度优化，确保一屏显示 */
@media screen and (max-height: 700px) {
  .header-gradient {
    padding: 16px;
  }
  
  .header-title {
    font-size: 16px;
  }
  
  .data-cards {
    margin-bottom: 16px;
  }
  
  .module-content {
    padding: 14px 8px;
  }
  
  .module-text {
    font-size: 10px;
  }
  
  .card-content {
    padding: 16px;
  }
  
  .module-item {
    margin-bottom: 12px;
  }
  
  .module-icon {
    margin-bottom: 8px;
  }
}

@media screen and (max-height: 650px) {
  .header-gradient {
    padding: 14px;
  }
  
  .header-title {
    font-size: 15px;
  }
  
  .data-cards {
    margin-bottom: 14px;
  }
  
  .module-content {
    padding: 12px 6px;
  }
  
  .module-text {
    font-size: 9px;
  }
  
  .card-content {
    padding: 14px;
  }
  
  .card-icon {
    width: 36px;
    height: 36px;
  }
  
  .card-value {
    font-size: 16px;
  }
  
  .card-label {
    font-size: 11px;
  }
  
  .module-item {
    margin-bottom: 10px;
  }
  
  .module-icon {
    margin-bottom: 6px;
  }
}

@media screen and (max-height: 600px) {
  .header-gradient {
    padding: 12px;
  }
  
  .header-title {
    font-size: 14px;
  }
  
  .data-cards {
    margin-bottom: 12px;
  }
  
  .module-content {
    padding: 10px 5px;
  }
  
  .module-text {
    font-size: 8px;
  }
  
  .card-content {
    padding: 12px;
  }
  
  .card-icon {
    width: 32px;
    height: 32px;
  }
  
  .card-value {
    font-size: 14px;
  }
  
  .card-label {
    font-size: 10px;
  }
  
  .module-item {
    margin-bottom: 8px;
  }
  
  .function-modules {
    padding: 0 12px;
  }
  
  .module-icon {
    margin-bottom: 6px;
  }
}

/* iPhone 14 专门适配 (390x844) */
@media screen and (width: 390px) and (height: 844px) {
  .dashboard-page {
    padding-top: max(env(safe-area-inset-top), 44px);
    height: calc(100vh - env(safe-area-inset-top, 0px));
  }
  
  .header-gradient {
    padding: 14px 16px;
  }
  
  .header-title {
    font-size: 15px;
  }
  
  .data-cards {
    margin: 0 12px 14px;
  }
  
  .card-content {
    padding: 12px;
    gap: 12px;
  }
  
  .card-icon {
    width: 36px;
    height: 36px;
  }
  
  .card-value {
    font-size: 15px;
  }
  
  .card-label {
    font-size: 11px;
  }
  
  .function-modules {
    padding: 0 12px;
    flex: 1;
    min-height: 0;
  }
  
  .module-content {
    padding: 10px 6px;
  }
  
  .module-text {
    font-size: 9px;
  }
  
  .module-item {
    margin-bottom: 8px;
  }
  
  .module-icon {
    margin-bottom: 6px;
  }
  
  /* 确保van-grid正确渲染 */
  :deep(.van-grid) {
    padding: 0;
  }
  
  :deep(.van-grid-item) {
    padding: 0;
  }
}

/* iPhone系列安全区域通用适配 */
@media screen and (max-width: 414px) {
  .dashboard-page {
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }
}

/* 高DPI屏幕优化 */
@media only screen and (-webkit-min-device-pixel-ratio: 2) {
  .card-icon,
  .module-icon {
    transform: translateZ(0);
    will-change: transform;
  }
  
  .data-card,
  .module-item {
    transform: translateZ(0);
  }
}

/* iPhone 14 Pro/Pro Max 特殊适配 */
@media screen and (device-width: 393px) and (device-height: 852px),
       screen and (device-width: 428px) and (device-height: 926px) {
  .header-gradient {
    padding: 18px 16px;
  }
  
  .data-cards {
    margin: 0 14px 18px;
  }
}

/* iPhone 14 特定设备像素比适配 */
@media screen and (device-width: 390px) and (device-height: 844px) and (-webkit-device-pixel-ratio: 3) {
  .dashboard-page {
    height: calc(100vh - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
    min-height: calc(100vh - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
    padding-bottom: max(60px, env(safe-area-inset-bottom, 20px));
  }
  
  .page-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-height: 0;
  }
  
  .header-section {
    margin-bottom: 12px;
    flex-shrink: 0;
  }
  
  .header-gradient {
    padding: 10px 16px;
  }
  
  .header-title {
    font-size: 14px;
  }
  
  .data-cards {
    margin: 0 10px 10px;
    flex-shrink: 0;
  }
  
  .card-content {
    padding: 8px;
    gap: 8px;
  }
  
  .card-icon {
    width: 28px;
    height: 28px;
  }
  
  .card-value {
    font-size: 13px;
    margin-bottom: 2px;
  }
  
  .card-label {
    font-size: 9px;
  }
  
  .function-modules {
    padding: 0 10px;
    flex: 1;
    min-height: 0;
    overflow: hidden;
  }
  
  .module-content {
    padding: 6px 3px;
  }
  
  .module-text {
    font-size: 7px;
    line-height: 1.1;
  }
  
  .module-item {
    margin-bottom: 4px;
  }
  
  .module-icon {
    margin-bottom: 3px;
  }
  
  :deep(.van-icon) {
    font-size: 16px !important;
  }
  
  :deep(.van-grid) {
    padding: 0;
    margin: 0;
  }
  
  :deep(.van-grid-item) {
    padding: 0;
    margin: 0;
  }
}

/* 横屏适配 */
@media screen and (orientation: landscape) and (max-height: 500px) {
  .dashboard-page {
    height: 100vh;
    overflow-y: auto;
  }
  
  .header-gradient {
    padding: 10px 16px;
  }
  
  .header-title {
    font-size: 14px;
  }
  
  .data-cards {
    margin-bottom: 10px;
  }
  
  .card-content {
    padding: 12px;
  }
  
  .module-content {
    padding: 8px 6px;
  }
  
  .module-text {
    font-size: 9px;
  }
}
</style>